﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;

namespace WebStudio.Framework.Helper
{
    /// <summary>
    /// 实体工具类
    /// </summary>
    public static class EntityHelper
    {
        /// <summary>
        /// DataSetToEntity
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dstSource"></param>
        /// <param name="tableIndex"></param>
        /// <returns></returns>
        public static List<T> DataSetToEntity<T>(this DataSet dstSource, int tableIndex)
            where T : class
        {
            //空值判断
            if (dstSource == null || dstSource.Tables.Count < 0)
                return default(List<T>);

            //空值判断
            if (dstSource.Tables[tableIndex].Rows.Count <= 0)
                return default(List<T>);

            //数据表
            DataTable datas = dstSource.Tables[tableIndex];

            // 返回值初始化
            List<T> result = new List<T>();
            for (int j = 0; j < datas.Rows.Count; j++)
            {
                T _T = (T)Activator.CreateInstance(typeof(T));
                PropertyInfo[] propertys = _T.GetType().GetProperties();

                foreach (PropertyInfo pi in propertys)
                {
                    // 属性与字段名称一致的进行赋值
                    if (datas.Columns.Contains(pi.Name))
                    {
                        // 数据库NULL值单独处理
                        if (datas.Rows[j][pi.Name] != DBNull.Value)
                        {
                            pi.SetValue(_T, datas.Rows[j][pi.Name], null);
                        }
                        else
                        {
                            pi.SetValue(_T, null, null);
                        }
                    } 
                }
                result.Add(_T);
            }
            return result;
        }

        public static List<T> DataReaderToEntity<T>(this IDataReader drSource)
            where T : class
        {
            //空值判断
            if (drSource == null)
                return default(List<T>);

            // 返回值初始化
            List<T> result = new List<T>();
            while (drSource.Read())
            {
                //创建实体对象
                T _T = (T)Activator.CreateInstance(typeof(T));
                PropertyInfo[] propertys = _T.GetType().GetProperties();

                //遍历实体属性集合
                foreach (PropertyInfo pi in propertys)
                {
                    for (int j = 0; j < drSource.FieldCount; j++)
                    {
                        // 属性与字段名称一致的进行赋值
                        if (drSource.GetName(j).Equals(pi.Name))
                        {
                            // 数据库NULL值单独处理
                            if (drSource[pi.Name] != DBNull.Value)
                            {
                                pi.SetValue(_T, drSource[pi.Name], null);
                            }
                            else
                            {
                                pi.SetValue(_T, null, null);
                            }
                        }
                    }
                }
                result.Add(_T);
            }

            return result;
        }
    }
}
